package android.framework.util;

import android.content.Context;
import android.framework.App;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.telephony.TelephonyManager;

public class NetWorkTypeUtils {
	public static final int NETTYPE_2G = 2;
	public static final int NETTYPE_3G = 3;
	public static final int NETTYPE_NO = 0;
	public static final int NETTYPE_WIFI = 1;

	public static NetworkInfo getAvailableNetWorkInfo(Context paramContext) {
		NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext
				.getSystemService("connectivity")).getActiveNetworkInfo();
		if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable()))
			return localNetworkInfo;
		return null;
	}

	public static int getNetType(Context paramContext) {
		if (paramContext == null) {
			paramContext = App.getInstance().getApplicationContext();
		}
		NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext
				.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
		if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable())) {
			int type = localNetworkInfo.getType();
			if(type == ConnectivityManager.TYPE_WIFI){
				return type;
			}
			if (type != ConnectivityManager.TYPE_WIFI) {
				switch (((TelephonyManager) paramContext
						.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType()) {
				case 3:
					return NETTYPE_3G;
				default:
					return NETTYPE_2G;
				}
			}
		}
		return NETTYPE_NO;

	}

	public static String getNetWorkType(Context paramContext) {
		String name = "";
		if (paramContext == null) {
			paramContext = App.getInstance().getApplicationContext();
		}
		NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext
				.getSystemService("connectivity")).getActiveNetworkInfo();
		if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable())) {
			int type = localNetworkInfo.getType();
			if (type != 1) {
				((TelephonyManager) paramContext.getSystemService("phone"))
						.getNetworkType();
			}
		}
		return name;
	}

	public static boolean isNetAvailable(Context paramContext) {
		if (getAvailableNetWorkInfo(paramContext) == null)
			return false;
		return true;
	}

	public static boolean isThirdGeneration(Context paramContext) {
		if (paramContext == null) {
			paramContext = App.getInstance().getApplicationContext();
		}
		((TelephonyManager) paramContext.getSystemService("phone"))
				.getNetworkType();
		return false;
	}

	public static boolean isWifi(Context paramContext) {
		int i = 1;
		if (paramContext == null) {
			paramContext = App.getInstance().getApplicationContext();
		}
		NetworkInfo localNetworkInfo = getAvailableNetWorkInfo(paramContext);
		if ((localNetworkInfo != null) && (localNetworkInfo.getType() == i))
			return true;
		return false;
	}
}